Page({
    data: {
        avatarUrl: '/icons/default-avatar.png',
        scanCodeResult: ''
    },

    onLoad() {
        // 从本地缓存加载用户信息
        const userInfo = wx.getStorageSync('userInfo') || {};
        if (userInfo.avatarUrl) {
            this.setData({
                avatarUrl: userInfo.avatarUrl,
            });
        }
    },

    // 当用户选择头像后触发
    onChooseAvatar(e) {
        const { avatarUrl } = e.detail; // 获取头像 URL
        this.setData({
            avatarUrl: avatarUrl
        });
        wx.setStorageSync('userInfo', {
            avatarUrl: avatarUrl
        });
    },

    scanCode() {
        wx.scanCode({
            success: (res) => {
                console.log('扫码成功', res);
                this.setData({
                    scanCodeResult: res.result
                });
            },
            fail: (err) => {
                console.log('扫码失败', err);
                if (err.errMsg.includes('auth deny')) {
                    wx.showToast({ title: '用户拒绝授权', icon: 'none' });
                }
            }
        });
    }
})